package cn.itsource.pet.service.impl;

import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.PetOnlineAuditLog;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.PetOnlineAuditLogMapper;
import cn.itsource.pet.service.IPetDetailService;
import cn.itsource.pet.service.IPetService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private IPetDetailService petDetailService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = null;
        if (pet.getDetail() == null){
            petDetail = new PetDetail();
        }else {
            petDetail = pet.getDetail();
        }

        petDetail.setPetId(pet.getId());
        petDetailService.add(petDetail);
    }

    @Override
    public void onsale(List<Long> ids) {
        // 1.我要过滤掉上架状态的数据，我要把数据转换为map，key就是主键，value就是对象
        Map<Long, Pet> petMaps = petMapper.findAll().stream().filter(pet -> pet.getState() == 0).collect(Collectors.toMap(Pet::getId, pet -> pet));
        Date date = new Date();
        for (Long id : ids) {
            Pet pet = petMaps.get(id);
            StringBuilder sb = new StringBuilder();
            // 不等于null,说明数据本身就是下架的
            if (pet != null){
                if (!BaiduAuditUtils.TextCensor(pet.getName())){
                    sb.append("宠物上架百度AI审核名称失败，宠物名称为：" + pet.getName() + ";");
                }
                if (pet.getResources().split(",").length > 1){
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources())) {
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");
                    }
                }
                if (sb.length() > 0){
                    PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                    petOnlineAuditLog.setAuditTime(date);
                    petOnlineAuditLog.setPetId(id);
                    petOnlineAuditLog.setNote(sb.toString());
                    petOnlineAuditLog.setState(0);
                    petOnlineAuditLogMapper.add(petOnlineAuditLog);
                    continue;
                }
                pet.setState(1L);
                pet.setOnsaletime(date);
                // 宠物上架之后，设置原来的夏季时间
                pet.setOffsaletime(null);
                petMapper.update(pet);
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        // 如果要判断，全部查询出来，只要状态是下架那么就不处理
        petMapper.batchOffsale(ids);
    }

    @Override
    public Pet getPetDetail(Long id) {
        return petMapper.getPetDetail(id);
    }
}
